package com.zsj.hello.five;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.zsj.hello.util.RabbimqFactoryUtil;

import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @author https://gitee.com/zhengshengjun
 * @date 2023/8/30.
 */
public class Producer_1 {
    public static final String QUEUE_NAME = "mq_queue_01";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbimqFactoryUtil.getRabbitMQChannel();
        //声明可持久化队列  队列虽然持久化了 但是队列中的消息丢失了
        //队列如果声明后 之前是未持久化的 必须删除之后重新声明一个持久化的队列  名字相同
//        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        //发送消息
        Scanner scan = new Scanner(System.in);
        System.out.println("消息等待发送中~~~");
        while (scan.hasNext()) {
            String message = scan.next();
//            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

            //如果要让队列持久化的同时消息也持久化 需要添加一个配置项目
            // MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            System.out.println("发送消息为" + message);
        }
    }

}
